<script>
import { ArrowDown } from "@element-plus/icons";
import router from "../router/router";
import TreeMenu from "./TreeMenu.vue";
import { Setting, Edit, Search, User } from "@element-plus/icons-vue";
import Breadcrumb from "./BreadCrumb.vue";

export default {
  components: {
    TreeMenu,
    setting: Setting,
    edit: Edit,
    search: Search,
    user: User,
    Breadcrumb
  },

  data() {
    return {
      userInfo: this.$store.state.userInfo,
      isCollapse: false,
      userMenu: [],
      noticeCount: 0,
    };
  },
  mounted() {
    this.getNoticeCount();
    this.getMenuList();
  },
  methods: {
    handleLogout(key) {
      // console.log(key);
      if (key == "email") return;
      this.$store.commit("saveUserInfo", "");
      this.userInfo = null;
      this.$router.push("/login");
    },
    toggle() {
      this.isCollapse = !this.isCollapse;
    },
    async getNoticeCount() {
      const res = await this.$api.noticeCount();
      // console.log(res);
      this.noticeCount = res;
    },
    async getMenuList() {
      const res = await this.$api.menuList();
      this.userMenu = res;
    },
  },
};
</script>

<template>
  <div class="basic-layout">
    <div :class="['nav-side', isCollapse ? 'fold' : 'unfold']">
      <div class="logo-box">
        <img src="../assets/vue.svg" alt="" />
        <div class="logo">Manager</div>
      </div>
      <el-menu
        default-active="2"
        class="nav-menu"
        background-color="#001529"
        text-color="#fff"
        :collapse="isCollapse"
        router
      >
        <TreeMenu :user-Menu="userMenu"></TreeMenu>
      </el-menu>
    </div>
    <!-- ['content-right', isCollapse ? 'fold' : 'unfold'] -->
    <div class="content-right">
      <div class="nav-top">
        <div class="bread">
          <Fold class="bread-icon" @click="toggle" />
          <div class="bread-text">
            <Breadcrumb></Breadcrumb>
          </div>
        </div>
        <div class="user-info">
          <el-badge :is-dot="noticeCount > 0 ? true : false" class="user-icon">
            <Bell class="user-icon-bell" />
          </el-badge>
          <el-dropdown @command="handleLogout">
            <span class="user-link">
              <div class="username">
                {{ userInfo.userName }}
              </div>
              <arrow-down />
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="email"
                  >邮箱:{{ userInfo.userEmail }}</el-dropdown-item
                >
                <el-dropdown-item command="logout">退出</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
      <div class="wrapper">
        <div class="main-page">
          <router-view></router-view>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
  overflow: hidden;
}
.basic-layout {
  width: 100%;
  height: 100vh;
  background-color: #dfdfdf;
  display: flex;
  .nav-side {
    width: 200px;
    height: 100vh;
    background-color: #001529;
    color: #fff;
    overflow-y: auto;
    transition: width 0.5s;
    .nav-menu {
      border-right: none;
      .nav-setting {
        width: 20px;
        height: 20px;
        margin-right: 10px;
      }
    }
    .logo-box {
      width: 100%;
      height: 50px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 10px;
      font-size: 18px;
      font-weight: 400px;
      font-family: "微软雅黑";
      img {
        width: 40px;
        height: 40px;
        // margin-right: 10px;
        flex-shrink: 0; // 防止图片被压缩
        object-fit: contain; // 保持图片比例
      }
      .logo {
        flex: 1;
        text-align: start;
        padding: 15px;
      }
    }
  }
  .fold {
    width: 64px;
  }
  .unfold {
    width: 200px;
  }
  .content-right {
    flex: 1;
    display: flex;
    flex-direction: column;
    .nav-top {
      height: 50px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 0 20px;
      background-color: #fff;
      border-bottom: 1px solid #eaeaea;
      padding: 0 20px;
      .bread {
        min-width: 90px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        color: #000;
        cursor: pointer;
        .bread-icon {
          width: 25px;
          height: 25px;
          margin-right: 10px;
        }
        
      }
      .user-info {
        max-width: 200px;
        height: 100%;
        display: flex;
        justify-content: space-between;
        align-items: center;

        cursor: pointer;
        .user-icon {
          width: 25px;
          display: flex;
          justify-content: center;
          align-items: center;
          overflow: visible;
          margin-right: 10px;
          .user-icon-bell {
            width: 20px;
            height: 20px;
          }
        }
        .el-dropdown {
          max-width: 120px;

          .user-link {
            display: flex;
            justify-content: space-between;
            align-items: center;
            .username {
              color: skyblue;
              white-space: nowrap; // 防止换行
              overflow: hidden; // 隐藏溢出部分
              text-overflow: ellipsis; // 显示省略号
              text-align: right; // 文本右对齐
            }
            svg {
              width: 15px;
              height: 15px;
              margin-left: 5px;
              flex-shrink: 0;
            }
          }
        }
      }
    }
    .wrapper {
      flex: 1;
      display: flex;
      padding: 20px;
      .main-page {
        flex: 1;
        background-color: #fff;
      }
    }
  }
}
</style>
